package com.xy.tianyu.module.picture.activity

import android.content.Context
import android.content.Intent
import androidx.core.content.ContextCompat
import androidx.lifecycle.rxLifeScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.fastjson.JSON
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ToastUtils
import com.xy.tianyu.BaseActivity
import com.xy.tianyu.R
import com.xy.tianyu.module.picture.adapter.PicSearchAdapter
import com.xy.tianyu.module.picture.bean.PicSearchBean
import com.xy.tianyu.module.picture.bean.PicSearchMultipleItem
import com.xy.tianyu.module.picture.bean.PicTodayHotMultipleItem
import com.xy.tianyu.network.Api
import kotlinx.android.synthetic.main.activity_pic_search.*
import rxhttp.RxHttp
import rxhttp.toStr

/**
 * @time 2020/10/14 13:56
 * @class 描述:
 */
class PicSearchActivity : BaseActivity() {
    private var pageIndex = 0
    private val pageNum = 30
    private lateinit var word:String
    private var picSearchList: MutableList<PicSearchMultipleItem> = mutableListOf()
    private lateinit var picSearchAdapter: PicSearchAdapter
    private val picVerList = mutableListOf<PicSearchBean.DataBean>()
    private val picSquareList = mutableListOf<PicSearchBean.DataBean>()

    companion object{
        const val INTENT_KEY_WORD = "intent_key_word"
        fun actionStart(context: Context,keyWord:String){
            val intent = Intent(context,PicSearchActivity::class.java)
            intent.putExtra(INTENT_KEY_WORD,keyWord)
            context.startActivity(intent)
        }
    }
    override fun getLayoutId(): Int {
        return R.layout.activity_pic_search
    }

    override fun initView() {
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this,R.color.white))

        iv_back.setOnClickListener {
            finish()
        }

        refresh_pic_search.setEnableRefresh(false)
        refresh_pic_search.setOnLoadMoreListener {
            pageIndex++
            request()
        }

        ry_search.layoutManager = LinearLayoutManager(this)
    }

    override fun initData() {
        word = intent.getStringExtra(INTENT_KEY_WORD)
        tv_title.text = word
        picSearchAdapter = PicSearchAdapter(picSearchList)
        ry_search.adapter = picSearchAdapter

        picSearchAdapter.addChildClickViewIds(R.id.iv_pic_today_hot_hor,R.id.iv_pic_today_hot_ver_one,R.id.iv_pic_today_hot_ver_two,
            R.id.iv_pic_today_hot_square_one,R.id.iv_pic_today_hot_square_two)
        picSearchAdapter.setOnItemChildClickListener { _, view, position ->
            val list = arrayListOf<PicSearchBean.DataBean>()
            val picSearchItem = picSearchList[position]
            var item : PicSearchBean.DataBean
            picSearchList.forEach {
                when (it.itemType) {
                    PicTodayHotMultipleItem.PIC_HOT_TODAY_HOR -> {
                        list.add(it.picSearchItem1)
                    }
                    else -> {
                        list.add(it.picSearchItem1)
                        list.add(it.picSearchItem2)
                    }
                }
            }

            when (view.id) {
                R.id.iv_pic_today_hot_hor -> {
                    item = picSearchItem.picSearchItem1
                }
                R.id.iv_pic_today_hot_ver_one -> {
                    item = picSearchItem.picSearchItem1
                }
                R.id.iv_pic_today_hot_ver_two -> {
                    item = picSearchItem.picSearchItem1
                }
                R.id.iv_pic_today_hot_square_one -> {
                    item = picSearchItem.picSearchItem1
                }
                else -> {
                    item = picSearchItem.picSearchItem1
                }
            }

            PicHotDayDetailActivity.actionStart2(this,1,list,item)
        }

        request()
    }

    private fun request(){
        rxLifeScope.launch({
            val picSearchStr = search()
            // TODO: 2020/10/14 这里用fastjson进行解析，Gson解析会报错
            val list = JSON.parseObject(picSearchStr,PicSearchBean::class.java).data
            if (!list.isNullOrEmpty()){
                list.forEach{
                    val width = it.width
                    val height = it.height
                    when{
                        width > height -> {
                            val multipleItem = PicSearchMultipleItem(
                                PicSearchMultipleItem.PIC_HOT_TODAY_HOR,
                                it,
                                list.random()
                            )
                            picSearchList.add(multipleItem)
                        }
                        width < height -> {
                            picVerList.add(it)
                            if (picVerList.size == 2) {
                                val multipleItem = PicSearchMultipleItem(
                                    PicSearchMultipleItem.PIC_HOT_TODAY_VER,
                                    picVerList[0],
                                    picVerList[1]
                                )
                                picSearchList.add(multipleItem)
                                picVerList.clear()
                            }
                        }
                        else -> {
                            picSquareList.add(it)
                            if (picSquareList.size == 2) {
                                val multipleItem = PicSearchMultipleItem(
                                    PicSearchMultipleItem.PIC_HOT_TODAY_SQUARE,
                                    picSquareList[0],
                                    picSquareList[1]
                                )
                                picSearchList.add(multipleItem)
                                picSquareList.clear()
                            }
                        }
                    }
                }

                picSearchAdapter.notifyDataSetChanged()
            }
        },{
            ToastUtils.showShort(it.message)
        },{

        },{
            refresh_pic_search.finishLoadMore()
        })
    }
    private suspend fun search():String{
        return RxHttp.get(Api.DAY_DAY_SEARCH_PICTURE_SEARCH_URL)
            .add("tn","resultjson")
            .add("ie","utf-8")
            .add("word",word)
            .add("pn",pageIndex)
            .add("rn",pageNum)
            .toStr()
            .await()
    }
}